package Survey;

import java.awt.Dimension;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DisplayGui extends JPanel{

	private static final long serialVersionUID = 594729552647032042L;

	public static final int MODULEWIDTH = 800;
	
	Vector<SurveyParameters> surveys; 
	Vector<DisplayModule> mods;
	
	public DisplayGui(Vector<SurveyParameters> surveys){
		
		this.surveys = surveys;
		int index = 1;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		mods = new Vector<DisplayModule>();
		DisplayModule working;
		for(SurveyParameters s : surveys){				
			if(s.type == SurveyParameters.OPEN_ENDED){
				working = new DModuleText(s, index);
			}else{
				working = new DModuleMultipleChoice(s, index);
			}
			add(working);
			mods.add(working);
			
			if(!s.multipart){
				index++;
			}
		}	
	}
		
	public String[][] getData(){
		
		String[][] retVal = new String[mods.size()][];
		
		String notAnswered = "";
		
		for(int i = 0; i < mods.size(); i++){
			retVal[i] = mods.get(i).getData();
			if(retVal[i] == null){
				notAnswered += (i+1) + ", ";
			}
		}
		
		if(!notAnswered.trim().equals("")){
			notAnswered = notAnswered.substring(0, notAnswered.length() - 2);
			if(notAnswered.length() > 1){
				notAnswered = notAnswered.substring(0, notAnswered.length() - (notAnswered.length() > 4 ? 2 : 3)) +
				" and " + notAnswered.substring(notAnswered.length() - 1);
			}
			
			int answer = JOptionPane.showConfirmDialog(this, "You have not answered question(s): " + notAnswered + 
					".\nAre you sure you would like to continue?\nRemember, you do not have to answer every question"
					, "Are you sure?", JOptionPane.YES_NO_OPTION);
			if(answer == JOptionPane.NO_OPTION){
				return null;
			}
		}
		
		return retVal;
		
	}
	
	
	public static void main(String[] args){
		
		Vector<SurveyParameters> surveys = null;
		
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream("surveys.dat"));
			surveys = (Vector<SurveyParameters>)in.readObject();
		}catch (FileNotFoundException e) {e.printStackTrace();}
		 catch (IOException e) {e.printStackTrace();} 
		 catch (ClassNotFoundException e) {e.printStackTrace();}
		 
		 JFrame y = new JFrame();
		 y.setSize(new Dimension(1000, 800));
		 
		 DisplayGui x = new DisplayGui(surveys);
		 y.add(x);
		 y.setVisible(true);
		 
	}
	
}
